# Consistency checks for the build configuration
{%-  import 'consistency_checks.jinja' as consistency_checks -%}

# Verify signature verification configuration
{{ consistency_checks.suit_verify_signature_check("RAD_RECOVERY", rad_recovery, sysbuild) }}

{%- set mpi_rad_recovery_vendor_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_RECOVERY_VENDOR_NAME']|default('nordicsemi.com') %}
{%- set mpi_rad_recovery_class_name = sysbuild['config']['SB_CONFIG_SUIT_MPI_RAD_RECOVERY_CLASS_NAME']|default('nRF54H20_sample_rad_recovery') %}
SUIT_Envelope_Tagged:
  suit-authentication-wrapper:
    SuitDigest:
      suit-digest-algorithm-id: cose-alg-sha-256
  suit-manifest:
    suit-manifest-version: 1
{%- if RAD_RECOVERY_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ RAD_RECOVERY_SEQ_NUM }}
{%- elif DEFAULT_SEQ_NUM is defined %}
    suit-manifest-sequence-number: {{ DEFAULT_SEQ_NUM }}
{%- else %}
    suit-manifest-sequence-number: 1
{%- endif %}
    suit-common:
      suit-components:
      - - MEM
        - {{ rad_recovery['dt'].label2node['cpu'].unit_addr }}
        - {{ get_absolute_address(rad_recovery['dt'].chosen_nodes['zephyr,code-partition']) }}
        - {{ rad_recovery['dt'].chosen_nodes['zephyr,code-partition'].regs[0].size }}
      - - CAND_IMG
        - 0
      suit-shared-sequence:
      - suit-directive-set-component-index: 0
      - suit-directive-override-parameters:
          suit-parameter-vendor-identifier:
            RFC4122_UUID: {{ mpi_rad_recovery_vendor_name }}
          suit-parameter-class-identifier:
            RFC4122_UUID:
                namespace: {{ mpi_rad_recovery_vendor_name }}
                name: {{ mpi_rad_recovery_class_name }}
          suit-parameter-image-digest:
            suit-digest-algorithm-id: cose-alg-sha-256
            suit-digest-bytes:
              file: {{ rad_recovery['binary'] }}
          suit-parameter-image-size:
            file: {{ rad_recovery['binary'] }}
      - suit-condition-vendor-identifier:
        - suit-send-record-success
        - suit-send-record-failure
        - suit-send-sysinfo-success
        - suit-send-sysinfo-failure
      - suit-condition-class-identifier:
        - suit-send-record-success
        - suit-send-record-failure
        - suit-send-sysinfo-success
        - suit-send-sysinfo-failure
      - suit-directive-set-component-index: 1
      - suit-directive-override-parameters:
          suit-parameter-image-digest:
            suit-digest-algorithm-id: cose-alg-sha-256
            suit-digest-bytes:
              file: {{ rad_recovery['binary'] }}
    suit-validate:
    - suit-directive-set-component-index: 0
    - suit-condition-image-match:
      - suit-send-record-success
      - suit-send-record-failure
      - suit-send-sysinfo-success
      - suit-send-sysinfo-failure
    suit-invoke:
    - suit-directive-set-component-index: 0
    - suit-directive-invoke:
      - suit-send-record-failure

{%- if RAD_RECOVERY_VERSION is defined %}
    suit-current-version: {{ RAD_RECOVERY_VERSION }}
{%- elif DEFAULT_VERSION is defined %}
    suit-current-version: {{ DEFAULT_VERSION }}
{%- endif %}

    suit-install:
    - suit-directive-set-component-index: 1
    - suit-directive-override-parameters:
        suit-parameter-uri: '#{{ rad_recovery['name'] }}'
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI' in rad_recovery['config'] and rad_recovery['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] != '' %}
        suit-parameter-uri: '{{ rad_recovery['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE_URI'] }}'
{%- else %}
        suit-parameter-uri: '#{{ rad_recovery['name'] }}'
{%- endif %}

    - suit-directive-fetch:
      - suit-send-record-failure
    - suit-condition-image-match:
      - suit-send-record-success
      - suit-send-record-failure
      - suit-send-sysinfo-success
      - suit-send-sysinfo-failure
    - suit-directive-set-component-index: 0
    - suit-directive-override-parameters:
        suit-parameter-source-component: 1
    - suit-directive-copy:
      - suit-send-record-failure
    - suit-condition-image-match:
      - suit-send-record-success
      - suit-send-record-failure
      - suit-send-sysinfo-success
      - suit-send-sysinfo-failure
    suit-manifest-component-id:
    - INSTLD_MFST
    - RFC4122_UUID:
        namespace: {{ mpi_rad_recovery_vendor_name }}
        name: {{ mpi_rad_recovery_class_name }}
{%- if 'CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE' not in rad_recovery['config'] or rad_recovery['config']['CONFIG_SUIT_DFU_CACHE_EXTRACT_IMAGE'] == '' %}
  suit-integrated-payloads:
    '#{{ rad_recovery['name'] }}': {{ rad_recovery['binary'] }}
{%- endif %}
